﻿/*
 * QWine Queue Machine (Special build for Immigration Office)
 * Copyright (C) 2010 Wish Enterprise Development Team

 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * 
 * Contact Us:
 * E-Mail   : contact@wishenterprise.com
 * 
 * */

using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using QWEntity.Local;
using QWModel;
using QWRepository;
using QWRepository.Local;
using QWTouchPanel.Properties;

namespace QWTouchPanel.Model
{
    /// <summary>
    /// QBerkasWidget : UserControl
    /// </summary>
    public partial class QWEntryPrint : UserControl
    {
        private bool _stillProcessing;

        /// <summary>
        /// Initializes a new instance of the <see cref="QWEntryPrint"/> class.
        /// </summary>
        public QWEntryPrint()
        {
            InitializeComponent();

            // set BACKGROUND
            //var t = new Thread(TBackgroundImage);
            //t.Start();
            Parallel.Invoke(TBackgroundImage);

            _stillProcessing = false;

        }

        /// <summary>
        /// Ts the background image.
        /// </summary>
        private void TBackgroundImage()
        {
            if (InvokeRequired)
            {
                var o = new DelegateThis(TBackgroundImage);
                Invoke(o);
            }
            else
            {
                BackgroundImage = Resources.widget_black480;
            }
        }


        /// <summary>
        /// Handles the MouseUp event of the qwButton1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
        private void qwButton1_MouseUp(object sender, MouseEventArgs e)
        {

            if (_stillProcessing) return;

            _stillProcessing = true;

            Layanan r = new LayananBase().GetById("A");

            if (r == null) return;

            // lock cek
            if (r.IsLocked)
            {

                var tLocked = new Thread(TLocked);
                tLocked.Start();
                Thread.Sleep(100);

                _stillProcessing = false;

                return;
            }

            // cetak
            var t = new Thread(PrintNomorPenyerahanBerkas);
            t.Start();
            Thread.Sleep(250);

            _stillProcessing = false;

        }

        /// <summary>
        /// Ts the locked.
        /// </summary>
        private void TLocked()
        {
            if (InvokeRequired)
            {
                var o = new DelegateThis(TLocked);
                Invoke(o);
            }
            else
            {
                using (var f = new QWaitingWidget("Maaf, layanan antrian ini tidak aktif ! "))
                {
                    f.Show(this);
                    Application.DoEvents();
                    Thread.Sleep(1000);
                }
            }
        }

        /// <summary>
        /// Prints the nomor penyerahan berkas.
        /// </summary>
        private void PrintNomorPenyerahanBerkas()
        {
            if (InvokeRequired)
            {
                var o = new DelegateThis(PrintNomorPenyerahanBerkas);
                Invoke(o);
            }
            else
            {
                string session = EnvironmentRepository.GetLocalSession();

                var nomor = new Berkas
                                {
                                    LayananID = "A",
                                    Session = session
                                };

                var db = new BerkasBase();
                int nResult = db.SaveByDml(nomor);

                if (nResult == 1)
                {
                    Berkas rs = db.GetById(session);

                    try
                    {
                        string nocetak =
                            string.Format("{0}-{1}", rs.LayananID,
                                          rs.LayananNomor.ToString().Trim().PadLeft(3, '0'));

                        /* ---------------------------
                         *  get copies configuration
                         * --------------------------- */
                        Layanan layanan = new LayananBase().GetById("A");

                        try
                        {
                            switch (Settings.Default.PrintingLayout)
                            {
                                case 0: // ESC
                                    QWPrinting.PrintWithESC(nocetak, "PENYERAHAN BERKAS", layanan.Copies, this);
                                    break;
                                case 1: // CR
                                    QWPrinting.PrintWithCRLayout(nocetak, "PENYERAHAN BERKAS", layanan.Copies, this);
                                    break;
                            }
                        }
                        catch (NullReferenceException)
                        {
                        }
                    }
                    catch (NullReferenceException)
                    {
                    }
                }
            }
        }

        #region Nested type: DelegateThis

        /// <summary>
        /// DelegateThis
        /// </summary>
        private delegate void DelegateThis();

        #endregion
    }
}